package com.mt.api.modules.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mt.api.constants.constant.Constants;
import com.mt.api.constants.enums.SizeSortNumEnum;
import com.mt.api.modules.domain.AppBossSetup;
import com.mt.api.modules.domain.bo.BossSetupBo;
import com.mt.api.modules.domain.vo.BossSetupVo;
import com.mt.api.modules.manager.IBossSetupManager;
import com.mt.api.modules.mapper.AppBossSetupMapper;
import com.mt.api.modules.service.AppBossSetupService;
import com.mt.api.util.JwtUserUtils;
import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @author plover
 * @description 服务实现
 * @date 2020-07-06
 **/
@Service
@RequiredArgsConstructor
public class AppBossSetupServiceImpl extends ServiceImpl<AppBossSetupMapper, AppBossSetup> implements AppBossSetupService {

    private final IBossSetupManager bossSetupManager;

    @Override
    public WebResponse getColorAndSize() {
        JSONObject result = new JSONObject();
        Integer bossId = JwtUserUtils.getJwtUser().getAppBoss().getId();
        AppBossSetup appBossSetup = bossSetupManager.getBossSetupByBossId(bossId);
        if (Objects.nonNull(appBossSetup)) {
            String colors = StringUtils.isEmpty(appBossSetup.getColors()) ? Constants.DEFAULT_COLORS : appBossSetup.getColors();
            String sizes = StringUtils.isEmpty(appBossSetup.getSizes()) ? Constants.DEFAULT_SIZES : appBossSetup.getSizes();
            List<String> colorList = Arrays.asList(colors.split(","));
            List<String> sizeList = Arrays.asList(sizes.split(","));
            result.put("colors", colorList);
            result.put("sizes", sizeList);
        }
        return WebResponseUtils.success(result);
    }

    @Override
    public WebResponse editSetUp(BossSetupBo bo) {
        Integer bossId = JwtUserUtils.getJwtUser().getAppBoss().getId();

        AppBossSetup appBossSetup = bossSetupManager.getBossSetupByBossId(bossId);
        AppBossSetup bossSetup = new AppBossSetup();
        if (CollectionUtils.isNotEmpty(bo.getColors())) {
            List<String> colorList = bo.getColors().stream().distinct().collect(Collectors.toList());
            bossSetup.setColors(JSONArray.toJSONString(colorList));
        }
        if (CollectionUtils.isNotEmpty(bo.getSizes())) {
            List<String> list = bo.getSizes().stream().distinct().map(s -> s.toUpperCase()).sorted(Comparator.comparing(SizeSortNumEnum::sortNumOfName)).collect(Collectors.toList());
            bossSetup.setSizes(JSONArray.toJSONString(list));
        }
        bossSetup.setQtyStepper(bo.getQtyStepper());
        bossSetup.setWarnVal(bo.getWarnVal());
        bossSetup.setBossId(bossId);
        if (Objects.isNull(appBossSetup)) {
            bossSetupManager.save(bossSetup);
        } else {
            bossSetup.setId(appBossSetup.getId());
            bossSetupManager.updateById(bossSetup);
        }
        return WebResponseUtils.success();
    }

    @Override
    public WebResponse getSetUp() {
        Integer bossId = JwtUserUtils.getJwtUser().getAppBoss().getId();
        AppBossSetup bossSetup = bossSetupManager.getBossSetupByBossId(bossId);
        if (Objects.nonNull(bossSetup)) {
            BossSetupVo vo = new BossSetupVo();
            vo.setId(bossSetup.getId());
            if (StringUtils.isNotEmpty(bossSetup.getSizes())) {
                vo.setColors(JSONArray.parseArray(bossSetup.getColors(), String.class));
            }
            if (StringUtils.isNotEmpty(bossSetup.getSizes())) {
                vo.setSizes(JSONArray.parseArray(bossSetup.getSizes(), String.class));
            }
            vo.setQtyStepper(bossSetup.getQtyStepper());
            vo.setWarnVal(bossSetup.getWarnVal());
            return WebResponseUtils.success(vo);
        }
        return WebResponseUtils.success();
    }

}